﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;

namespace HCFramework.Master.Normal
{
    public partial class BreadScrumb : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetDefaultBreadScrumb();
                SetBreadScrumbForSpecialPages();
            }
        }

        private void SetBreadScrumbForSpecialPages()
        {
            string currPath = Request.Url.AbsolutePath;
            string currUrl = Request.Url.AbsoluteUri;

            if (M.UrlQuery.categoryID.HasValue && !M.UrlQuery.productID.HasValue)
            {
                List<BreadScrumbItem> datas = new List<BreadScrumbItem>();

                BreadScrumbItem homeItem = new BreadScrumbItem();
                homeItem.Text = "Trang chủ";
                homeItem.Url = "/Default.aspx";
                datas.Add(homeItem);

                ProductCategory category = C.ProductCategoryController.GetByID(M.UrlQuery.categoryID.Value);
                BreadScrumbItem item2 = new BreadScrumbItem();
                item2.Text = category.Name;
                item2.Url = currUrl;
                datas.Add(item2);

                litBreadScrumb.Text = GetDisplay(datas);
            }

            if (M.UrlQuery.productID.HasValue)
            {
                List<BreadScrumbItem> datas = new List<BreadScrumbItem>();

                BreadScrumbItem homeItem = new BreadScrumbItem();
                homeItem.Text = "Trang chủ";
                homeItem.Url = "/Default.aspx";
                datas.Add(homeItem);

                API.LINQ.Product product = C.ProductController.GetByID(M.UrlQuery.productID.Value);
                BreadScrumbItem item1 = new BreadScrumbItem();
                item1.Text = product.ProductCategory.Name;
                item1.Url = "/Product/Default.aspx?categoryID=" + product.CategoryID;
                datas.Add(item1);

                BreadScrumbItem item2 = new BreadScrumbItem();
                item2.Text = product.ProductName;
                item2.Url = currUrl;
                datas.Add(item2);

                litBreadScrumb.Text = GetDisplay(datas);
            }

            if (M.UrlQuery.newsID.HasValue)
            {
                List<BreadScrumbItem> datas = new List<BreadScrumbItem>();

                BreadScrumbItem homeItem = new BreadScrumbItem();
                homeItem.Text = "Trang chủ";
                homeItem.Url = "/Default.aspx";
                datas.Add(homeItem);

                API.LINQ.New news = C.NewsController.GetByID(M.UrlQuery.newsID.Value);
                BreadScrumbItem item1 = new BreadScrumbItem();
                item1.Text = "Dự án";
                item1.Url = "/News/Listing.aspx";
                datas.Add(item1);

                BreadScrumbItem item2 = new BreadScrumbItem();
                item2.Text = news.Title;
                item2.Url = currUrl;
                datas.Add(item2);

                litBreadScrumb.Text = GetDisplay(datas);
            }
        }

        private void SetDefaultBreadScrumb()
        {
            List<BreadScrumbItem> datas = new List<BreadScrumbItem>();

            BreadScrumbItem homeItem = new BreadScrumbItem();
            homeItem.Text = "Trang chủ";
            homeItem.Url = "/Default.aspx";
            datas.Add(homeItem);

            if (!C.TrangController.IsHomePage())
            {
                BreadScrumbItem currItem = new BreadScrumbItem();
                currItem.Text = this.Page.Title;
                currItem.Url = Request.Url.AbsolutePath;
                datas.Add(currItem);
            }

            litBreadScrumb.Text = GetDisplay(datas);
        }

        private string GetDisplay(List<BreadScrumbItem> datas)
        {
            string ret = "";

            foreach (BreadScrumbItem item in datas)
            {
                string temp = "<a href='{url}' class='hcLink'>{text}</a>";
                string separate = datas.First() == item ? "" : " > ";
                temp = temp.Replace("{url}", item.Url);
                temp = temp.Replace("{text}", item.Text);
                ret += separate + temp;
            }

            return ret;
        }
    }

    public class BreadScrumbItem
    {
        public string Text { get; set; }
        public string Url { get; set; }
    }
}